package org.example.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.example.entity.User;
import org.example.mapper.UserMapper;
import org.example.service.UserService;
import org.example.util.MD5Util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @ClassName UserServiceImpl
 * @Description TODO
 * @Author qiuye
 * @Date 2025年08月13日 15:07
 * @Version 1.0
 */
@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;



    @Override
    public int register(String account, String password,String nickname,String address) {
        password =  MD5Util.encode(password);
        int registered = userMapper.registerUser(account, password, nickname, address);
        return registered;
    }

    @Override
    public User login(String account, String password) {
        password =  MD5Util.encode(password);
        return userMapper.findByAccountAndPassword(account, password);
    }

    @Override
    public int addUser(User user) {
        user.setPassword(MD5Util.encode(user.getAccount()+user.getPassword()));
        return userMapper.addUser(user);
    }

    @Override
    public int updateUser(User user) {
        return userMapper.updateUser(user);
    }

    @Override
    public int deleteUser(int id) {
        return userMapper.deleteUser(id);
    }

    @Override
    public User findUserById(int id) {
        return userMapper.findUserById(id);
    }

    @Override
    public PageInfo<User> findUsers(User user, int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<User> users = userMapper.findUsers(user);
        PageInfo<User> pageInfo = new PageInfo<>(users);
        return pageInfo;
    }

    @Override
    public PageInfo<User> selectUserAndQuestion(User user, int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<User> users = userMapper.selectUserAndQuestion(user);
        PageInfo<User> pageInfo = new PageInfo<>(users);
        return pageInfo;
    }

    @Override
    public int updatePassword(int userId, String newPassword) {
        // 对密码进行加密处理
        String encryptedPassword = MD5Util.encode(newPassword);
        return userMapper.updatePassword(userId, encryptedPassword);
    }

    @Override
    public int updatePhone(Integer userId, String phone) {
        return userMapper.updatePhone(userId, phone);
    }


}
